package main

import (
	"fmt"
	user "gin/domain"
	"gin/service"
	"gin/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

//全局，用于校验cookie，验证登录状态
var u1 user.Tb_user
var ad user.Tb_admin

func main() {
	r := gin.Default()
	//中间件，cookie检查，这块没想好项目结构布局，写得太臃肿。。
	r.Use(checkCookie())
	//渲染HTML
	LoadHTML(r)
	//渲染一些需要接收参数后端的HTML
	LoadH(r)
	//用户登录
	login(r)
	//管理员登录
	admin(r)
	//用户信息修改
	modify(r)
	//删除用户
	deleteUser(r)
	//注册用户
	register(r)
	//adModify(r)
	r.Run(":8080")
}
func LoadHTML(r *gin.Engine) {
	r.LoadHTMLGlob("web/**/*")
	r.GET("/welcome/login", func(c *gin.Context) {
		c.HTML(http.StatusOK, "login.html", gin.H{})
	})
	r.GET("/welcome/error", func(c *gin.Context) {
		c.HTML(http.StatusOK, "error.html", gin.H{})
	})
	r.GET("/admin/admin", func(c *gin.Context) {
		c.HTML(http.StatusOK, "admin.html", gin.H{})
	})
	r.GET("/welcome/register", func(c *gin.Context) {
		c.HTML(http.StatusOK, "register.html", gin.H{})
	})
}
func LoadH(r *gin.Engine) {
	r.LoadHTMLGlob("web/**/*")
	r.GET("/main/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"name": u1.Name,
			"sex":  u1.Sex,
			"home": u1.Home,
			"age":  u1.Age,
		})
	})
	r.GET("/settings/user", func(c *gin.Context) {
		c.HTML(http.StatusOK, "user.tmpl", gin.H{
			"sex":  u1.Sex,
			"home": u1.Home,
			"age":  u1.Age,
		})
	})
	//backToWeb()
	r.GET("/main/manage", func(c *gin.Context) {
		list := getUserList(r)
		c.HTML(http.StatusOK, "manage.tmpl", gin.H{
			"user": list,
		})
	})
}

//保存cookie
func saveUserCookie(c *gin.Context) {
	str := utils.ToMd5(u1.Name + u1.Pwd)
	c.SetCookie("user", str, 60, "/", "localhost", false, true)
	cookie, _ := c.Cookie("user")
	fmt.Println(cookie)
}
func saveAdminCookie(c *gin.Context) {
	str := utils.ToMd5(ad.Name + ad.Pwd)
	c.SetCookie("admin", str, 60, "/", "localhost", false, true)
	cookie, _ := c.Cookie("admin")
	fmt.Println(cookie)
}
func login(r *gin.Engine) {
	r.POST("welcome/login.do", func(c *gin.Context) {
		name := c.PostForm("name")
		pwd := c.PostForm("pwd")
		md5str := utils.ToMd5(pwd)
		flag, user := service.Login(name, md5str)
		fmt.Println(name, ":", pwd)
		if flag == true {
			c.Redirect(http.StatusMovedPermanently, "/main/index")
			fmt.Println(user)
			u1 = user
			saveUserCookie(c)
			//用户数据发给前端页面
			//r.GET("/main/index", func(c *gin.Context) {
			//	c.JSON(http.StatusOK,user)
			//})
		} else {
			c.Redirect(http.StatusMovedPermanently, "/welcome/error")
		}
	})
}

//func test() {
//	files, err := template.ParseFiles("../web/main/index.html")
//	if err!=nil{
//		panic(err)
//	}
//
//}
func getUserList(r *gin.Engine) []user.Tb_user {
	return service.GetUserList()
}
func deleteUser(r *gin.Engine) {
	r.GET("main/delete.do", func(c *gin.Context) {
		var form user.Tb_user
		c.ShouldBind(&form)
		fmt.Println(form)
		flag := service.Delete(form)
		fmt.Println(flag)
		c.Redirect(http.StatusMovedPermanently, "/main/manage")
	})
}
func admin(r *gin.Engine) {
	r.POST("admin/admin.do", func(c *gin.Context) {
		name := c.PostForm("name")
		pwd := c.PostForm("pwd")
		md5str := utils.ToMd5(pwd)
		flag, admin := service.Admin(name, md5str)
		fmt.Println(name, ":", pwd)
		if flag == true {
			ad = admin
			saveAdminCookie(c)
			c.Redirect(http.StatusMovedPermanently, "/main/manage")
			fmt.Println(admin)
			//用户数据发给前端页面
			//r.GET("/main/index", func(c *gin.Context) {
			//	c.JSON(http.StatusOK,user)
			//})
		} else {
			c.Redirect(http.StatusMovedPermanently, "/welcome/error")
		}
	})
}

//func backToWeb()  {
//	http.HandleFunc("/main/index",indexWeb)
//err := http.ListenAndServe(":8080", nil)
//if err!=nil{
//	panic(err)
//}

//}
//func indexWeb(w http.ResponseWriter,r *http.Request)  {
//	t, err := template.ParseFiles("D:\\Go项目\\awesomeProject\\web\\main\\index.tmpl")
//	fmt.Println("-----------")
//	if err!=nil{
//		panic(err)
//	}
//	t.Execute(w,u1)
////}
//func adModify(r *gin.Engine) {
//	var newForm user.Tb_user
//	r.POST("main/adModify.do", func(c *gin.Context) {
//		err:= c.ShouldBind(&newForm)
//		if err!=nil{
//			panic(err)
//		}
//		fmt.Println(newForm)
//		service.AdModify(u1.Id,newForm)
//	})
//}
func modify(r *gin.Engine) {
	var newForm user.Tb_user
	r.POST("settings/modify.do", func(c *gin.Context) {
		err := c.ShouldBind(&newForm)
		if err != nil {
			panic(err)
		}
		fmt.Println(newForm)
		fmt.Println(u1)
		_, user := service.Modify(u1, newForm)
		u1 = user
		c.Redirect(http.StatusMovedPermanently, "/main/index")
	})
}

//中间件，验证cookie
func checkCookie() gin.HandlerFunc {
	return func(c *gin.Context) {
		nowUser := utils.ToMd5(u1.Name + u1.Pwd)
		cookieUser, _ := c.Cookie("user")
		nowAdmin := utils.ToMd5(ad.Name + ad.Pwd)
		cookieAdmin, _ := c.Cookie("admin")
		path := c.FullPath()
		fmt.Println(path)
		if cookieAdmin == nowAdmin || cookieUser == nowUser || path == "/welcome/login" || path == "/welcome/login.do" || path == "/admin/admin.do" || path == "/admin/admin" || path == "/welcome/register" || path == "/welcome/register.do" {
			c.Next()
			return
		}
		c.Redirect(http.StatusMovedPermanently, "/welcome/login")
		c.Abort()
	}
}
func register(r *gin.Engine) {
	var u user.Tb_user
	r.POST("welcome/register.do", func(c *gin.Context) {
		fmt.Println("执行到了")
		c.ShouldBind(&u)
		fmt.Println(u)
		flag := service.Register(u)
		_, user := service.Login(u.Name, utils.ToMd5(u.Pwd))
		if flag == true {
			u1 = user
			c.Redirect(http.StatusMovedPermanently, "/main/index")
		}
	})
}
